import pygame

TILE_SIZE = 32

TILE_TYPES = {
    'B': {"name": "brick", "image": "images/brick.png", "durability": 1, "passable": False, "destroyable": True},
    'I': {"name": "iron", "image": "images/iron.png", "durability": -1, "passable": False, "destroyable": False},
    'R': {"name": "river", "image": "images/river.png", "durability": -1, "passable": False, "destroyable": False},
    'T': {"name": "tree", "image": "images/tree.png", "durability": -1, "passable": True, "destroyable": False},
    '.': None
}


class Tile:
    def __init__(self, kind, x, y):
        self.kind = kind
        self.x = x
        self.y = y
        self.config = TILE_TYPES.get(kind)
        if self.config:
            self.image = pygame.image.load(self.config["image"])
            self.rect = self.image.get_rect(topleft=(x * TILE_SIZE, y * TILE_SIZE))
            self.durability = self.config["durability"]
            self.destroyable = self.config["destroyable"]
        else:
            self.image = None
            self.rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            self.durability = -1
            self.destroyable = False

    def draw(self, screen):
        if self.image:
            screen.blit(self.image, self.rect)

    def hit(self):
        if self.destroyable:
            self.durability -= 1
            if self.durability <= 0:
                return True
        return False


def get_default_map():
    return [
        list("IIIIIIIIIIIIIIIIIIIIIIIII"),
        list("IBBBBBBBBBBBBBBBBBBBBBBBI"),
        list("IB.....................BI"),
        list("IB..III...RRRRRR...II..BI"),
        list("IB..III...RTT......II..BI"),
        list("IB.........R...........BI"),
        list("IB..BBBBBBB.....BBB....BI"),
        list("IB..BB...BB.....B......BI"),
        list("IB..BB.H.BB.....B......BI"),
        list("IB..BBBBBBB.....BBB....BI"),
        list("IB.....................BI"),
        list("IB..TTT.........III....BI"),
        list("IB..TTT.........III....BI"),
        list("IB.....................BI"),
        list("IB......BBB........BBB.BI"),
        list("IB.....................BI"),
        list("IB..............BBB....BI"),
        list("IB..........H..........BI"),
        list("IIIIIIIIIIIIIIIIIIIIIIIII")
    ]


class GameMap:
    def __init__(self, map_data):
        self.tiles = []
        self.home = None
        self.home_destroyed = False
        self.load_map(map_data)

    def load_map(self, map_data):
        for y, row in enumerate(map_data):
            tile_row = []
            for x, char in enumerate(row):
                if char == 'H':
                    from src.elements.home import Home
                    self.home = Home(x * TILE_SIZE, y * TILE_SIZE)
                tile_row.append(Tile(char, x, y))
            self.tiles.append(tile_row)

    def draw(self, screen):
        for row in self.tiles:
            for tile in row:
                tile.draw(screen)
        if self.home:
            self.home.draw(screen)

    def check_collision(self, rect):
        for row in self.tiles:
            for tile in row:
                if tile.config and not tile.config["passable"] and tile.rect.colliderect(rect):
                    return tile
        return None

    def bullet_hit(self, bullet_rect):
        for row in self.tiles:
            for tile in row:
                if tile.config and tile.rect.colliderect(bullet_rect):
                    if tile.hit():
                        tile.kind = '.'
                        tile.config = None
                        tile.image = None
                        return True
        if self.home and self.home.rect.colliderect(bullet_rect):
            if self.home.hit():
                self.home_destroyed = True
                return True
        return False

    def check_home_destroyed(self):
        return self.home_destroyed